//
//  LSZCacheHandling.m
//  JKFuYinProject
//
//  Created by 九酷 on 2017/10/31.
//  Copyright © 2017年 LC. All rights reserved.
//

#import "LSZCacheHandling.h"

@implementation LSZCacheHandling
#pragma mark - 查看里面有没有数据
+(void)initWithfindPlist:(NSString *)strPlistName :(plistData)block
{
    // 1.获得沙盒根路径
    NSFileManager * fileManager = [NSFileManager defaultManager];
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path1 = [pathArray objectAtIndex:0];
    NSString * strName = [NSString stringWithFormat:@"%@.plist",strPlistName];
    NSString *myPath = [path1 stringByAppendingPathComponent:strName];
    NSDictionary * diction = [[NSDictionary alloc]initWithContentsOfFile:myPath];
 
    BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:myPath];
    if (blHave == YES) {
        //存在这个文件夹
        if (diction.allKeys.count >0) {
            NSString * timeStr = [NSString stringWithFormat:@"%@",diction[@"plistTime"]];
          NSString * stringTime  =  [LSZSpecialPackaging GetThePreviousTimeAndCompareItNow:timeStr];
            if([stringTime rangeOfString:@"天"].location != NSNotFound ||[stringTime rangeOfString:@"月"].location != NSNotFound||[stringTime rangeOfString:@"年"].location != NSNotFound)
            {
                //有对应的文件夹
                [LSZSpecialPackaging Reachability:^(NSString * strMessage) {
                    if ([strMessage isEqualToString:NoNetwork]) {
                        //没有网络的情况下
                        block(YES,diction);//删除失败
                    }else{
                    //进行删除
                        BOOL blDele= [fileManager removeItemAtPath:myPath error:nil];
                        if (blDele) {
                            block(NO,nil);//删除成功
                        }else {
                            block(YES,diction);//删除失败
                        }
                    }
                }];
            }else{
                 block(YES,diction);//缓存在有效期之内
            }
        }else{
            block(NO,diction);//没有数据
        }
    }else{
        block(NO,diction);//不存在这个文件夹
      }
}
#pragma mark - 删除对应文件夹的数据
+(void)initWithdeletingPlist:(NSString *)strPlistName :(plistData)block
{
    NSFileManager* fileManager=[NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    //文件名
    NSString * strName = [NSString stringWithFormat:@"%@.plist",strPlistName];
    NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:strName];
    BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];
    if (!blHave) {
        //不存在对应的文件夹
        block(YES,nil);
    }else {
        //有对应的文件夹
        BOOL blDele= [fileManager removeItemAtPath:uniquePath error:nil];
        if (blDele) {
            block(YES,nil);//删除成功
         }else {
             block(NO,nil);//删除失败
        }
    }
}
#pragma mark - 把请求到的数据添加进去 添加不为空判断
+(void)initAddPathPraisePoetry :(NSString *)strPlistName NSDictionary:(NSDictionary *)dic
{
    NSMutableDictionary * mutDic = [[NSMutableDictionary alloc]initWithDictionary:dic];
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [path objectAtIndex:0];
    NSString * strName = [NSString stringWithFormat:@"%@.plist",strPlistName];
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:strName];
    //获取当前的时间
    NSString * GetTheCurrentMonthDay = [LSZSpecialPackaging GetTheCurrentMonthDay];
    [mutDic setValue:GetTheCurrentMonthDay forKey:@"plistTime"];
    if ([[mutDic valueForKey:@"data"] isKindOfClass:[NSString class]]) {
        return;
    }
    if ([[mutDic valueForKey:@"data"] isKindOfClass:[NSNumber class]]) {
        return;
    }
    if (![mutDic valueForKey:@"data"]) {
        return ;
    }
    NSMutableArray *  arrayDat = [[NSMutableArray alloc]initWithArray:mutDic[@"data"]];
     for ( int A = 0; A <arrayDat.count; A ++) {
         NSDictionary *diction = mutDic[@"data"][A];
         NSMutableDictionary * mutDicParticulars = [[NSMutableDictionary alloc]initWithDictionary:diction];
        NSArray *arrayKey = mutDicParticulars.allKeys;
        NSArray * arrayValue = mutDicParticulars.allValues;
        for (int i = 0; i<arrayKey.count; i++) {
            NSString * strValue = [NSString stringWithFormat:@"%@",arrayValue[i]];
            if ([strValue  rangeOfString:@"null"].location !=NSNotFound) {
                NSString * strKey = [NSString stringWithFormat:@"%@",arrayKey[i]];
                strValue=[strValue stringByReplacingOccurrencesOfString:@"<null>"withString:@""];
                [mutDicParticulars setObject:@"" forKey:strKey];
                NSMutableDictionary * dicXiuGai = [[NSMutableDictionary alloc]initWithDictionary:mutDicParticulars];
                [arrayDat removeObjectAtIndex:A];
                [arrayDat  insertObject:dicXiuGai atIndex:A];
            }
        }
    }
    [mutDic setObject:arrayDat forKey:@"data"];
    BOOL didWriteSuccessfull = [mutDic writeToFile:plistPath atomically:YES];
    if (didWriteSuccessfull == YES) {
        NSLog(@"缓存成功");
    }else{
        NSLog(@"缓存失败");
    }
}
#pragma mark - 查看是否有缓存 YYCache
+(void)seeIfThereIsCache:(NSString *)LargeDirectory :(NSString *)SmallDirectory :(strArray)block
{
    YYCache *cache = [YYCache cacheWithName:LargeDirectory];
    NSMutableArray * arrayData = [[NSMutableArray alloc]init];

    if ([cache containsObjectForKey:SmallDirectory]) {
        arrayData =(NSMutableArray *)[cache objectForKey:SmallDirectory];
        block(YES,arrayData);//有缓存  返回缓存的数据
    }
    else{
        block(NO,nil);//没有缓存
    }
}
#pragma mark - 添加数据做缓存
+(void)addDataForCachingWithDic:(NSString *)LargeDirectory SmallDirectory:(NSString *)SmallDirectory dic:(NSDictionary *)dic
{
    if ([[dic valueForKey:@"data"] isKindOfClass:[NSString class]]) {
        return  ;
    }
    if ([[dic valueForKey:@"data"] isKindOfClass:[NSNumber class]]) {
        return  ;
    }
    if (![dic valueForKey:@"data"]) {
        return  ;
    }
    NSArray * arrayData = dic[@"data"];
    [LSZCacheHandling addDataForCaching:LargeDirectory SmallDirectory:SmallDirectory array:arrayData];
}
#pragma mark - 添加数据做缓存
+(void)addDataForCaching:(NSString *)LargeDirectory SmallDirectory:(NSString *)SmallDirectory array:(NSArray *)array
{
    YYCache *cache = [YYCache cacheWithName:LargeDirectory];
    [cache.diskCache setCostLimit:10*1024];//磁盘最大缓存开销
    [cache.diskCache setAutoTrimInterval:60*60*24*1*100];//设置磁盘lru动态清理频率 默认 60秒
    NSMutableArray * arrayData = [[NSMutableArray alloc]init];
    arrayData =(NSMutableArray *)[cache objectForKey:SmallDirectory];
    NSMutableArray * arrayAddData = [[NSMutableArray alloc]init];
    [arrayAddData addObjectsFromArray:arrayData];
    //看看新获取数据之前有没有获取到   有的话就过滤掉 没的话就添加上
    for (NSDictionary * dic in array) {
        int Z = 0;
        if (arrayData.count >0) {
            for (int i = 0; i<arrayData.count; i++) {
                if ([dic isEqualToDictionary:arrayData[i]]) {
                    Z  = 1;
                }
            }
            if (Z == 0 ) {
                [arrayAddData  addObject:dic];//把没有添加的数据添加上去
            }
        }else{
            arrayAddData = (NSMutableArray *)array;
            break;
        }
    }
    if (arrayAddData.count - arrayData.count >0) {
        [cache setObject:arrayAddData forKey:SmallDirectory];
    }
}
#pragma mark - 把传过来的dic直接做缓存
+(void)addDicDataCachedDirectly :(NSString *)strPlistName NSDictionary:(NSDictionary *)dic
{
    NSMutableDictionary * mutDic = [[NSMutableDictionary alloc]initWithDictionary:dic];
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [path objectAtIndex:0];
    NSString * strName = [NSString stringWithFormat:@"%@.plist",strPlistName];
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:strName];
    //获取当前的时间
    NSString * GetTheCurrentMonthDay = [LSZSpecialPackaging GetTheCurrentMonthDay];
    [mutDic setValue:GetTheCurrentMonthDay forKey:@"plistTime"];
    if ([[mutDic valueForKey:@"data"] isKindOfClass:[NSString class]]) {
        return  ;
    }
    if ([[mutDic valueForKey:@"data"] isKindOfClass:[NSNumber class]]) {
        return  ;
    }
    if (![mutDic valueForKey:@"data"]) {
        return  ;
    }
    BOOL didWriteSuccessfull = [mutDic writeToFile:plistPath atomically:YES];
    if (didWriteSuccessfull == YES) {
        NSLog(@"缓存成功");
    }else{
        NSLog(@"缓存失败");
    }
}
#pragma mark - 修改某个数据的某个参数 做判断的key 应该为的value       需要改变的key是parameter  改变的value是modifiedValue
+(void)modifyingParameterOfData:(NSString *)LargeDirectory SmallDirectory:(NSString *)SmallDirectory key:(NSString *)key value:(NSString *)value parameter:(NSString *)parameter modifiedValue:(NSString *)modifiedValue
{
    YYCache *cache = [YYCache cacheWithName:LargeDirectory];
    [cache.diskCache setCostLimit:10*1024];//磁盘最大缓存开销
    [cache.diskCache setAutoTrimInterval:60*60*24*1*100];//设置磁盘lru动态清理频率 默认 60秒
    NSMutableArray * arrayData = [[NSMutableArray alloc]init];
    arrayData =(NSMutableArray *)[cache objectForKey:SmallDirectory];
    NSMutableArray * arrayAddData = [[NSMutableArray alloc]init];
    //    [arrayAddData addObjectsFromArray:arrayData];
    //看看新获取数据之前有没有获取到   有的话就过滤掉 没的话就添加上
    for (NSDictionary * dic in arrayData) {
        //        int Z = 0;
        NSString * valueStr = [NSString stringWithFormat:@"%@",dic[key]];
        if ([valueStr isEqualToString:value]) {
            NSMutableDictionary * dicChange = [[NSMutableDictionary alloc]initWithDictionary:dic];
            //找到需要改变的对应的字典
            //            [arrayAddData removeObject:dic];
            [dicChange setValue:modifiedValue forKey:parameter];
            [arrayAddData addObject:dicChange];
        }else{
            [arrayAddData addObject:dic];
        }
    }
    [cache setObject:arrayAddData forKey:SmallDirectory];
}
@end
